﻿using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {
	public static Vector3 direction;
	public GameObject objFollowing;
	Vector3 drawPos0;	//拖拽起点
	Vector3 drawPos1;	//拖拽终点
	float smooth = 500f;
	// Use this for initialization
	void Start () {
		//direction = transform.position - objFollowing.transform.position;
		direction = new Vector3(0,10,-10);
	}
	
	// Update is called once per frame
	void LateUpdate () {
		//if (objFollowing) {
		if (false) {
			transform.position = Vector3.Lerp (transform.position, direction + objFollowing.transform.position,
				smooth * Time.deltaTime);
		//} else if (NetworkManager.selfPlayer) {
		} else if (false){
			objFollowing = NetworkManager.selfPlayer;
			transform.position = Vector3.Lerp (transform.position, direction + objFollowing.transform.position,
				smooth * Time.deltaTime);
		} else if (true) {
			if (Input.GetKeyDown (KeyCode.Mouse1)) {
				drawPos0 = getMousePos ();
			}
			if (Input.GetKey (KeyCode.Mouse1)) {
				drawPos1 = getMousePos ();
				transform.position = Vector3.Lerp (transform.position, transform.position + (drawPos0 - drawPos1),
							smooth * Time.deltaTime);
			}
		}
	}

	public static Vector3 getMousePos(){
		Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//从相机所在点到鼠标在屏幕上的点的射线
		RaycastHit floorHit;//代表射线被 截断点

		//Raycast（ray，out hitpoint，distance，layermask）函数--是在distance范围内，如果射线ray射中了可被射中层内
		//（layermask代表的层）的物体，则返回true，并且将射中点包含的一系列信息赋给 hitpoint（RaycastHit类型）---一个**结构体**类型
		if (Physics.Raycast (camRay, out floorHit, 100f, LayerMask.GetMask ("Floor"))) {
			return floorHit.point;
		} else {
			return (Camera.main.transform.position-CameraFollow.direction);
		}
	}
}
